package pl.edu.wsinf.lukaszwalczak.emailclient.view;

import java.util.HashMap;
import java.util.Map;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.controller.AccountCreateController;

@Component
public class AccountCreateView {

    @Inject
    private AccountCreateController controller;
    private Stage stage = null;

    public void show() {
        if (stage == null) {
            stage = new Stage();
            //resizable disabled
            stage.setResizable(false);
            //create root node of scene, i.e. group
            AnchorPane root = new AnchorPane();
            //create scene with set width, height and color
            Scene scene = new Scene(root, 480, 320, Color.WHITESMOKE);
            //set scene to stage
            stage.setScene(scene);
            stage.setTitle("Nowe konto email");
            //center stage on screen
            stage.centerOnScreen();
            //show the stage
            stage.show();

            int LEFT_PADDING = 20;
            int RIGHT_PADDING = 20;
            int TOP_PADDING = 15;
            int BOTTOM_PADDING = 15;
            int FIELD_SPACING = 25;
            int LABEL_WIDTH = 210;
            int TEXT_FIELD_WIDTH = 250;

            // Pole "Adres email"
            Label emailLabel = new Label("Adres email:");
            emailLabel.setPrefWidth(LABEL_WIDTH);
            final TextField emailTextField = new TextField();
            emailTextField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "Hasło"
            Label passLabel = new Label("Hasło:");
            passLabel.setPrefWidth(LABEL_WIDTH);
            final PasswordField passTextField = new PasswordField();
            passTextField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "Powtórz hasło"
            Label passLabel2 = new Label("Powtórz hasło:");
            passLabel2.setPrefWidth(LABEL_WIDTH);
            final PasswordField passTextField2 = new PasswordField();
            passTextField2.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "Nazwa użytkownika"
            Label usernameLabel = new Label("Nazwa użytkownika:");
            usernameLabel.setPrefWidth(LABEL_WIDTH);
            final TextField usernameTextField = new TextField();
            usernameTextField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "Serwer poczty przychodzącej"
            Label incomingServerLabel = new Label("Serwer poczty przychodzącej:");
            incomingServerLabel.setPrefWidth(LABEL_WIDTH);
            final TextField incomingServerTextField = new TextField();
            incomingServerTextField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "Protokol"
            Label incomingServerProtocolLabel = new Label("Protokół poczty przychodzącej:");
            incomingServerProtocolLabel.setPrefWidth(LABEL_WIDTH);
            final ChoiceBox incomingServerProtocolChoiseBox = new ChoiceBox();
            incomingServerProtocolChoiseBox.getItems().addAll("POP3", "IMAP");
            incomingServerProtocolChoiseBox.setValue("IMAP");
            incomingServerProtocolChoiseBox.setPrefWidth(100);

            // Pole "Port"
            Label incomingServerPortLabel = new Label("Port:");
            incomingServerPortLabel.setPrefWidth(50);
            final TextField incomingServerPortTextField = new TextField();
            incomingServerPortTextField.setPrefWidth(100);

            // Pole "Port"
            Label incomingServerAuthLabel = new Label("Szyfrowanie poczty przychodzącej:");
            incomingServerAuthLabel.setPrefWidth(LABEL_WIDTH);
            final ChoiceBox incomingServerAuthChoiceBox = new ChoiceBox();
            incomingServerAuthChoiceBox.getItems().addAll("NO_AUTH", "SSLTLS", "STARTTLS");
            incomingServerAuthChoiceBox.setValue("SSLTLS");
            incomingServerAuthChoiceBox.setPrefWidth(100);

            // Pole "Serwer poczty wychodzącej"
            Label outgoingServerLabel = new Label("Serwer poczty wychodzącej:");
            outgoingServerLabel.setPrefWidth(LABEL_WIDTH);
            final TextField outgoingServerTextField = new TextField();
            outgoingServerTextField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "Protokol"
            Label outgoingServerProtocolLabel = new Label("Protokół poczty wychodzącej:");
            outgoingServerProtocolLabel.setPrefWidth(LABEL_WIDTH);
            final ChoiceBox outgoingServerProtocolChoiseBox = new ChoiceBox();
            outgoingServerProtocolChoiseBox.getItems().addAll("SMTP");
            outgoingServerProtocolChoiseBox.setValue("SMTP");
            outgoingServerProtocolChoiseBox.setPrefWidth(100);

            // Pole "Port"
            Label outgoingServerPortLabel = new Label("Port:");
            outgoingServerPortLabel.setPrefWidth(50);
            final TextField outgoingServerPortTextField = new TextField();
            outgoingServerPortTextField.setPrefWidth(100);

            // Pole "Port"
            Label outgoingServerAuthLabel = new Label("Szyfrowanie poczty wychodzącej:");
            outgoingServerAuthLabel.setPrefWidth(LABEL_WIDTH);
            final ChoiceBox outgoingServerAuthChoiceBox = new ChoiceBox();
            outgoingServerAuthChoiceBox.getItems().addAll("NO_AUTH", "SSLTLS", "STARTTLS");
            outgoingServerAuthChoiceBox.setValue("SSLTLS");
            outgoingServerAuthChoiceBox.setPrefWidth(100);

            // Przycisk "Dodaj"
            Button btn = ViewHelper.createButton("Dodaj", new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    Map<String, String> values = new HashMap<>();
                    values.put("email", emailTextField.getText());
                    values.put("password", passTextField.getText());
                    values.put("username", usernameTextField.getText());
                    values.put("incomingServer", incomingServerTextField.getText());
                    values.put("incomingServerProtocol", incomingServerProtocolChoiseBox.getValue().toString());
                    values.put("incomingServerPort", incomingServerPortTextField.getText());
                    values.put("incomingServerAuth", incomingServerAuthChoiceBox.getValue().toString());
                    values.put("outgoingServer", outgoingServerTextField.getText());
                    values.put("outgoingServerProtocol", outgoingServerProtocolChoiseBox.getValue().toString());
                    values.put("outgoingServerPort", outgoingServerPortTextField.getText());
                    values.put("outgoingServerAuth", outgoingServerAuthChoiceBox.getValue().toString());
                    controller.createAccount(values);
                    stage.close();
                }
            }, null, 100d, null);

            root.getChildren().addAll(emailLabel, emailTextField,
                    passLabel, passTextField,
                    passLabel2, passTextField2,
                    usernameLabel, usernameTextField,
                    incomingServerLabel, incomingServerTextField,
                    incomingServerProtocolLabel, incomingServerProtocolChoiseBox,
                    incomingServerPortLabel, incomingServerPortTextField,
                    incomingServerAuthLabel, incomingServerAuthChoiceBox,
                    outgoingServerLabel, outgoingServerTextField,
                    outgoingServerProtocolLabel, outgoingServerProtocolChoiseBox,
                    outgoingServerPortLabel, outgoingServerPortTextField,
                    outgoingServerAuthLabel, outgoingServerAuthChoiceBox,
                    btn);

            AnchorPane.setTopAnchor(emailLabel, Double.valueOf(TOP_PADDING));
            AnchorPane.setLeftAnchor(emailLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(emailTextField, Double.valueOf(TOP_PADDING));
            AnchorPane.setLeftAnchor(emailTextField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(passLabel, Double.valueOf(TOP_PADDING + 1 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(passLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(passTextField, Double.valueOf(TOP_PADDING + 1 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(passTextField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(passLabel2, Double.valueOf(TOP_PADDING + 2 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(passLabel2, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(passTextField2, Double.valueOf(TOP_PADDING + 2 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(passTextField2, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(usernameLabel, Double.valueOf(TOP_PADDING + 3 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(usernameLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(usernameTextField, Double.valueOf(TOP_PADDING + 3 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(usernameTextField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(incomingServerLabel, Double.valueOf(TOP_PADDING + 4 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(incomingServerTextField, Double.valueOf(TOP_PADDING + 4 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerTextField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(incomingServerProtocolLabel, Double.valueOf(TOP_PADDING + 5 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerProtocolLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(incomingServerProtocolChoiseBox, Double.valueOf(TOP_PADDING + 5 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerProtocolChoiseBox, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(incomingServerPortLabel, Double.valueOf(TOP_PADDING + 5 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerPortLabel, Double.valueOf(LABEL_WIDTH + 110));
            AnchorPane.setTopAnchor(incomingServerPortTextField, Double.valueOf(TOP_PADDING + 5 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerPortTextField, Double.valueOf(LABEL_WIDTH + 150));
            AnchorPane.setTopAnchor(incomingServerAuthLabel, Double.valueOf(TOP_PADDING + 6 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerAuthLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(incomingServerAuthChoiceBox, Double.valueOf(TOP_PADDING + 6 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(incomingServerAuthChoiceBox, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(outgoingServerLabel, Double.valueOf(TOP_PADDING + 7 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(outgoingServerTextField, Double.valueOf(TOP_PADDING + 7 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerTextField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(outgoingServerProtocolLabel, Double.valueOf(TOP_PADDING + 8 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerProtocolLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(outgoingServerProtocolChoiseBox, Double.valueOf(TOP_PADDING + 8 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerProtocolChoiseBox, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(outgoingServerPortLabel, Double.valueOf(TOP_PADDING + 8 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerPortLabel, Double.valueOf(LABEL_WIDTH + 110));
            AnchorPane.setTopAnchor(outgoingServerPortTextField, Double.valueOf(TOP_PADDING + 8 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerPortTextField, Double.valueOf(LABEL_WIDTH + 150));
            AnchorPane.setTopAnchor(outgoingServerAuthLabel, Double.valueOf(TOP_PADDING + 9 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerAuthLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(outgoingServerAuthChoiceBox, Double.valueOf(TOP_PADDING + 9 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(outgoingServerAuthChoiceBox, Double.valueOf(LABEL_WIDTH));

            AnchorPane.setBottomAnchor(btn, Double.valueOf(BOTTOM_PADDING));
            AnchorPane.setRightAnchor(btn, Double.valueOf(RIGHT_PADDING));

        } else {
            stage.show();
        }
    }
}
